//
//  DiscoverVC.swift
//  ListenFM
//
//  Created by szq on 2017/8/7.
//  Copyright © 2017年 胡定锋. All rights reserved.
//

import UIKit
import LLCycleScrollView

class DiscoverVC: BaseScene,UITableViewDataSource,UITableViewDelegate {
    
    var tableView = UITableView()
    var bannerView: LLCycleScrollView?
    
    var headDataArray = NSMutableArray()
    var authorDataArray: NSMutableArray?
    var titleArray = NSMutableArray()
    var imageArray = NSMutableArray()
    var bannerPath = NSMutableArray()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "发现"
        
        initData()
        
        setUpViews()
        
        getRequest()
        
    }
    
    func setUpViews(){
        
        let rect = CGRect.init(x: 0, y: 0, width: SCR_WIDTH, height: SCR_HEIGHT)
        tableView = UITableView.init(frame: rect, style: UITableViewStyle.grouped)
        tableView.dataSource = self
        tableView.delegate = self
        view.addSubview(tableView)
        
    }
    
    func initData(){
        
        for i in 1..<10 {
            let str = String(format:"emotion%d",i)
            imageArray.add(str)
        }
        for i in 1...9 {
            let str = String(format:"scene%d",i)
            imageArray.add(str)
        }
        titleArray = ["烦躁","悲伤","孤独","弃疗","减压","无奈","快乐","感动","迷茫","睡前","旅行","散步","坐车","独处","失恋","失眠","随便","无聊"]
    }
    
    func getRequest(){
        
        DownHeadData.loadData(params: nil, success: { (model) in
            
            self.headDataArray = (model.data as? NSMutableArray)!
            for model in self.headDataArray{
                
                self.bannerPath.add((model as! HeadDataModel).cover)
            }
            
            self.tableView.reloadData()
            
        }) { (error) in
            
        }
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 0 {
            
            let indentifier = "Dis_BannertableCell"
            var cell = tableView.dequeueReusableCell(withIdentifier: indentifier)
            if cell == nil {
                
                cell = Dis_BannerCell(style: UITableViewCellStyle.default, reuseIdentifier: indentifier)
            }
            
            return cell!
        }
        if indexPath.section == 1 {
            
            let identifier = "Dis_CategorytableCell"
            var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
            if(cell == nil){
                
                cell = Dis_CategoryCell(style: UITableViewCellStyle.value1, reuseIdentifier: identifier) as Dis_CategoryCell
                
                
            }
            return cell!
        }
        if indexPath.section == 2 {
            
            let identifier = "Dis_AuthorCollectionCell"
            var cell = tableView.dequeueReusableCell(withIdentifier: identifier)
            if cell == nil {
                
                cell = Dis_AuthorCell.init(style: UITableViewCellStyle.default, reuseIdentifier: identifier)
            }
            return cell!
        }
        return UITableViewCell()
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        switch indexPath.section {
        case 0:
            return 40
        case 1:
            let width = (2 * SCR_WIDTH - 4*10)/6;
            return 3 * width
        case 2:
            return CGFloat(5 * 70 + 50 + 25)
        default:
            break
        }
        
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        
        if section == 0 {
            
            return 210
            
        }else {
            
            return 0.01
        }
        
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let rect = CGRect.init(x: 0, y: 0, width: SCR_WIDTH, height: 200)
        if section == 0 {

            bannerView = LLCycleScrollView.init(frame: rect)
            bannerView?.imagePaths = bannerPath as! Array<String>
            
            // header与content之间留下10个间隙
            let viewContainer = UIView.init(frame: rect)
            viewContainer.addSubview(bannerView!)
        
            return viewContainer
        }
        
        return nil
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.section == 0 {
            print("跳转到搜索页面")
        }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        
    }
    

}
